package com.jinlejia.erp.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.freedom.yefeng.yfrecyclerview.YfListAdapter
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder
import com.jinlejia.erp.R
import com.jinlejia.erp.databinding.ItemFollowUpDetailsBinding
import com.jinlejia.erp.utils.DateUtil

/**
 * @author SongJiaTai
 * @description:跟进详情Adapter
 * @date :2020/1/14 15:01
 */
class FollowUpDetailsAdapter  constructor(data:ArrayList<HashMap<String,Any>>) : YfListAdapter<HashMap<String, Any>>(data){
    private lateinit var mContext: Context
    private var listener:OnItemListener? = null
    override fun onCreateDataViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
        mContext = parent!!.context.applicationContext
        var view = LayoutInflater.from(parent.context).inflate(R.layout.item_follow_up_details,parent,false)
        return ViewHolder(view)
    }

    override fun onCreateEmptyViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
        val view =
            LayoutInflater.from(parent!!.context).inflate(R.layout.empty_layout, parent, false)
        val txt = view.findViewById<TextView>(R.id.id_tips)
        txt.text = "暂无数据"
        return YfSimpleViewHolder(view)
    }

    override fun onBindDataViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        val binding = DataBindingUtil.getBinding<ItemFollowUpDetailsBinding>(holder!!.itemView)
        val map = data[position]
        binding!!.map = map
        binding.tvFollowTime.text = DateUtil.getTime(map["followTime"].toString().toLong())
        holder.itemView.setOnClickListener {
            listener!!.onClick(map)
        }
    }

    override fun onCreateLoadingViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
        val view =
            LayoutInflater.from(parent!!.context).inflate(R.layout.loading_layout, parent, false)
        return YfSimpleViewHolder(view)
    }

    class ViewHolder constructor(view: View) : RecyclerView.ViewHolder(view){
        init {
            DataBindingUtil.bind<ItemFollowUpDetailsBinding>(view)
        }
    }

    fun setItemListener(onItemListener:OnItemListener){
        this.listener=onItemListener
    }

    interface OnItemListener {
        fun onClick(map:HashMap<String,Any>)
    }
}